﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using EARS.App_Code;

namespace EARS
{
    public partial class CreateAccount : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnCreate_Click(object sender, EventArgs e)
        {
            MatricNoDB m = new MatricNoDB();
            bool check = m.checkMatricNo(txtMatric.Text);
            if (check == true)
            {
                Account a = new Account();
                if (a.CheckMatric(txtMatric.Text) == false)
                {
                    try
                    {
                        a.CreateAcc(new Account(txtUserID.Text, txtPassword.Text, txtName.Text, txtMatric.Text, txtEmail.Text, int.Parse(txtContact.Text), ddlCourse.SelectedValue));
                        lblPopUp.Text = "Account has been successfully created!";
                        mpe.Show();
                    }
                    catch (SqlException ex)
                    {
                        lblPopUp.Text = "ERROR: Unable to create account. Please try again.";
                        mpe.Show();
                    }
                }
                else
                {
                    lblPopUp.Text = "ERROR: Matric card No. is already in use. Please try again.";
                }
            }
            else
            {
                lblPopUp.Text = "ERROR: Matric No. does not exist. Please try again.";
                mpe.Show();
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Home.aspx");
        }

        protected void btnClose_Click(object sender, EventArgs e)
        {
            if (lblPopUp.Text.Equals("Account has been successfully created!"))
            {
                mpe.Hide();
                Response.Redirect("~/Home.aspx");
            }
            else
            {
                mpe.Hide();
            }
        }
    }
}